package test.gdata.docs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class DocumentClient {
	private DocsService service;
	
	private String username;
	
	
	private final String URL_HOST = "https://docs.google.com";
	private final String URL_DEFAULT = "/feeds/default";
	private final String URL_DOCLIST_FEED = "/private/full";
	private final String URL_CATEGORY_KML = "/-/kml";
	
	public DocumentClient(String username, String password) throws AuthenticationException{
		service = new DocsService("docsServiceClient");
		try {
			service.setUserCredentials(username, password);
			this.username = username;
		} catch (AuthenticationException e) {
			throw e;
		}
	}
	
	public DocumentListFeed getDocsListFeed(DocType type) throws IOException, ServiceException{
		DocumentListFeed docListFeed = null;
		URL url = buildDocListFeedUrl(type);
		if(url != null){
			docListFeed = service.getFeed(url,  DocumentListFeed.class);
		}
		return docListFeed;
	}
	
	private URL buildDocListFeedUrl(DocType docType) throws MalformedURLException{
		switch (docType) {
		case All:
			return new URL(URL_HOST + URL_DEFAULT + URL_DOCLIST_FEED);
		case Folder:
			return new URL(URL_HOST + URL_DEFAULT + URL_DOCLIST_FEED + "showfolders=true");
		case KML:
			return new URL(URL_HOST + URL_DEFAULT + URL_DOCLIST_FEED + URL_CATEGORY_KML);
		default:
			return null;
		}
	}
}
